package com.example.mybatisplus.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.service.AdminInfoService;
import com.example.mybatisplus.model.domain.AdminInfo;

import javax.xml.ws.spi.http.HttpExchange;
import java.util.Arrays;
import java.util.List;
/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2023-10-16
 * @version v1.0
 */
@CrossOrigin
@RestController
@RequestMapping("/api/adminInfo")
public class AdminInfoController {
    private final Logger logger = LoggerFactory.getLogger( AdminInfoController.class );
    @Autowired
    private AdminInfoService adminInfoService;
    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        AdminInfo  adminInfo =adminInfoService.getById(id);
        return JsonResponse.success(adminInfo);
    }


    /**
     * 描述：根据admin_phone查询
     *
     */
    @RequestMapping("getAdminInfoByPhone")
    @ResponseBody
    public  JsonResponse  getAdminInfoByPhone(String admin_phone) {
        List<AdminInfo> s=adminInfoService.getAdminInfoByPhone(admin_phone);
        return JsonResponse.success(s);
    }
    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping("deleteAdminById")
    @ResponseBody
    public  JsonResponse  deleteAdminById( AdminInfo adminId) {

       Integer s=adminInfoService.deleteAdminById(adminId);
        return JsonResponse.success(s);
    }

    /**
     * 描述：管理员登录
     *
     */
    @RequestMapping("adminLogin")
    @ResponseBody
    public JsonResponse AdminLogin(String adminPhone,String adminPassword){

        System.out.println(adminPassword);
        System.out.println(adminPhone);
          AdminInfo login=adminInfoService.AdminLogin(adminPhone,adminPassword);
        List<AdminInfo> s=adminInfoService.getAdminInfoByPhone(adminPhone);
        System.out.println(s);
        System.out.println(login);
        if(login!=null){
            return JsonResponse.success(login);
        }
        else if(s.isEmpty()){
            return JsonResponse.message(false,"账号不存在");
        }
        else{
            return JsonResponse.message(false,"账号密码不匹配");
        }


    }
    /**
     * 查询所有users，即管理员，初访员，助理
     *
     */
    @RequestMapping("getAllUsers")
    public JsonResponse getAllUsers(){
        List<AdminInfo> s=adminInfoService.getAllUsers();
        return JsonResponse.success(s);
    }
    /**
     * 管理员Info 分页
     *
     */
    @RequestMapping("findAdminByPageAndLimit")
    public JsonResponse findAdminByPageAndLimit(Integer page,Integer limit){
        List<AdminInfo> s=adminInfoService.findAdminByPageAndLimit(page,limit);
        return JsonResponse.success(s);
    }

    /**
     * 描述：插入admin信息
     *
     */
    @PostMapping("addAdmins")
    @ResponseBody
//    @RequestMapping(value = "addAdmins", method = RequestMethod.POST)
    public JsonResponse addAdmins(AdminInfo admin){
       try{
           Integer s=adminInfoService.addAdmins(admin);
           return JsonResponse.success(s);
       }catch (Exception e){
         return JsonResponse.message(false,"工号或手机号重复!!!");
       }
    }

    /**
     * 描述：通过ID更新用户信息
     *
     */
    @PostMapping("updateAdminById")
    @ResponseBody
    //    @RequestMapping(value = "addAdmins", method = RequestMethod.POST)
    public JsonResponse updateAdminById(AdminInfo admin){
            try {
                Integer s=adminInfoService.updateAdminById(admin);
                return JsonResponse.success(s);
            }catch (Exception e){
                return JsonResponse.message(false,"工号或手机号重复重复!!!");
            }
    }
    /**
     * 描述：批量删除admin信息
     *
     */
    @PostMapping("deleteAdmins")
    @ResponseBody
    public JsonResponse deleteAdmins( @RequestParam(value = "ids[]") String[]  ids) {
        for (int i = 0; i < ids.length; i++) {
            System.out.println(ids[i]);
        }
      Integer s=adminInfoService.deleteAdmins(ids);
        return  JsonResponse.success(s);
    }
    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateAdminInfo(AdminInfo  adminInfo) throws Exception {
        adminInfoService.updateById(adminInfo);
        return JsonResponse.success(null);
    }

    /**
     * 描述：通过名称查询
     *
     */
    @RequestMapping("findAdminByName")
    @ResponseBody
    public JsonResponse findAdminByName(String  adminName) throws Exception {
      List<AdminInfo> s= adminInfoService.findAdminByName(adminName);
      return JsonResponse.success(s);
    }

    /**
    * 描述:创建AdminInfo
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(AdminInfo  adminInfo) throws Exception {
        adminInfoService.save(adminInfo);
        return JsonResponse.success(null);
    }
}

